/*
 * Created by JFormDesigner on Tue Jan 31 11:54:54 CET 2012
 */

package SMSGUI;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.*;

import jxl.read.biff.BiffException;

import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

/**
 * @author Erik Egmond
 */
public class GUI extends JPanel {
	HashMap<Integer, String[]> excelData=null;
	HashMap<HashMap<String, Integer>, String[]>trainresults;
	Image image;
	public GUI() {
		initComponents();
		
	}

	//train
	private void button1MouseClicked(MouseEvent e) {
		if(excelData!=null){
			message.setBackground(Color.yellow);
			message.setText("Training");
			
			String [] data;
			String [] toTrain = new String[2];
			int number;
			HashMap<Integer, String[]> tweetSentimentMap = new HashMap<Integer, String[]>();
			Iterator<Integer> kExcelData = excelData.keySet().iterator();
			while (kExcelData.hasNext()){
				number = kExcelData.next();
				data = excelData.get(number);
				toTrain[0] = data[1];
				toTrain[1] = data[3];
				tweetSentimentMap.put(number, toTrain);				
			}
			
			
			trainresults = Functions.train(tweetSentimentMap);

		}else{
			message.setBackground(Color.red);
			message.setText("No file selected");
			
		}
	}
	
	//classify
	private void button2MouseClicked(MouseEvent e) {
		message.setBackground(Color.yellow);
		message.setText("Classifying");
		String [] data;
		String [] toClassify = new String[2];
		int number;
		HashMap<Integer, String[]> tweetSentimentMap = new HashMap<Integer, String[]>();
		Iterator<Integer> kExcelData = excelData.keySet().iterator();
		while (kExcelData.hasNext()){
			number = kExcelData.next();
			data = excelData.get(number);
			toClassify[0] = data[1];
			toClassify[1] = null;
			tweetSentimentMap.put(number, toClassify);				
		}
		
		tweetSentimentMap = Functions.classify(tweetSentimentMap);

		
	}

	//read file
	private void button3MouseClicked(MouseEvent e) {
		message.setBackground(Color.white);	
		message.setText("opening file...");
		//file, tweet, sentiment, name, province, city
		try {
			excelData = ReadExcel.readFiveColumns(textField1.getText(), 9, 4, 11, 15, 16);
			message.setBackground(Color.white);	
			message.setText("file opened");	
		} catch (IOException e1) {
			message.setBackground(Color.red);
			message.setText("Error opening file...");	
		} catch (BiffException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Erik Egmond
		textField1 = new JTextField();
		readfile = new JButton();
		message = new JTextField();
		trainButton = new JButton();
		button2 = new JButton();
		label4 = new JLabel();
		amountweets = new JTextField();
		label1 = new JLabel();
		textField2 = new JTextField();
		label2 = new JLabel();
		textField3 = new JTextField();

		//======== this ========

		// JFormDesigner evaluation mark
		setBorder(new javax.swing.border.CompoundBorder(
			new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
				"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
				javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
				java.awt.Color.red), getBorder())); addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

		setLayout(new FormLayout(
			"default, 2*($lcgap, 50dlu), 8*($lcgap, default)",
			"11*(default, $lgap), default"));

		//---- textField1 ----
		textField1.setText("trainSet.xls");
		add(textField1, CC.xywh(3, 3, 4, 1));

		//---- readfile ----
		readfile.setText("Read File");
		readfile.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				button3MouseClicked(e);
			}
		});
		add(readfile, CC.xy(9, 3));

		//---- message ----
		message.setEditable(false);
		add(message, CC.xywh(3, 5, 3, 1));

		//---- trainButton ----
		trainButton.setText("Train");
		trainButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				button1MouseClicked(e);
			}
		});
		add(trainButton, CC.xy(9, 5));

		//---- button2 ----
		button2.setText("Classify");
		button2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				button2MouseClicked(e);
			}
		});
		add(button2, CC.xy(11, 5));

		//---- label4 ----
		label4.setText("#tweets");
		add(label4, CC.xy(3, 9));
		add(amountweets, CC.xy(5, 9));

		//---- label1 ----
		label1.setText("% Correct");
		add(label1, CC.xy(3, 21));

		//---- textField2 ----
		textField2.setEditable(false);
		add(textField2, CC.xy(5, 21));

		//---- label2 ----
		label2.setText("% Wrong");
		add(label2, CC.xy(3, 23));

		//---- textField3 ----
		textField3.setEditable(false);
		add(textField3, CC.xy(5, 23));
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Erik Egmond
	private JTextField textField1;
	private JButton readfile;
	private JTextField message;
	private JButton trainButton;
	private JButton button2;
	private JLabel label4;
	private JTextField amountweets;
	private JLabel label1;
	private JTextField textField2;
	private JLabel label2;
	private JTextField textField3;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
